package com.base2.webshop.client.views.widgets;

import com.base2.webshop.client.presenters.LoginPresenter;
import com.base2.webshop.client.uihandlers.MainUiHandlers;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

public class LoginPanel extends ViewWithUiHandlers<MainUiHandlers> implements LoginPresenter.ILoginView {

	
	private final Widget widget;
	
	public interface Binder extends UiBinder<Widget, LoginPanel> {
	}

	@UiField
	TextBox login_name;
	@UiField
	PasswordTextBox login_password;
	@UiField
	Anchor registerLink;
	@UiField
	Anchor forgot_password;
	@UiField
	Button login_button;

	@Inject
	public LoginPanel(Binder binder) {
		widget=binder.createAndBindUi(this);
	}
	
	@Override
	public Widget asWidget() {
		return widget;
	}

	@UiHandler("login_password")
	void onPasswordTextFocus(KeyUpEvent e) {
		changeVisibilityOfAnchor();
	}

	@UiHandler("login_password")
	void onPasswordTextBlur(BlurEvent e) {
		changeVisibilityOfAnchor();
	}

	private void changeVisibilityOfAnchor() {
		forgot_password.setVisible(login_password.getText().isEmpty());
	}

	@UiHandler("login_button")
	void onLoginClicked(ClickEvent e) {
		if (!login_name.getText().isEmpty()) {
			Window.alert("Welcome, " + login_name.getText());
		}
		login_name.setText("");
		login_password.setText("");
		forgot_password.setVisible(true);
	}

	@UiHandler("registerLink")
	void onRegisterLinkClicked(ClickEvent e) {
		Window.alert("Please Register");
	}

	@UiHandler("forgot_password")
	void onForgotPasswordClick(ClickEvent e) {
		Window.alert("How can you forget your own password?");
	}

	

}
